define(function(require, exports, module){
	
	/* 封装素材分类方法 */
	
	var initClassify = function(option){
		var op = $.extend({
			container: 'body',
			data: {ungroup:[{id:1, name:"素材1", tags:["电器","海尔"]}, {id:2, name:"素材2", tags:["家具","宜家"]}], group:[{name:"分组1", weight:12, materials:[{id:3, name:"素材3", tags:["电器","美的"]}]}, {name:"分组2", weight:13, materials:[{id:4, name:"素材4", tags:["电器","国美"]}, {id:5, name:"素材5", tags:["洁具","TOTO"]}]}]},
			callback: null
		},option);
		//$.extend(op.data,{ungroup:[], group:[]});
		
		var $node = (typeof op.container=='string'?$(op.container):op.container);
		if($node.size()<1)return;
		
		var tmp_box = ['<div class="classifybox">',
				           	'<div class="panel panel-success clfy_group">',
					    		'<div class="panel-heading">',
					    			'<h3 class="panel-title">分组列表</h3>',
					    			'<a class="clfy_addgroup btn btn-default btn-xs" href="javascript:;">新增分组</a>',
					    			'<input type="text" class="clfy_newgroup">',
					    	    '</div>',
					    		'<div class="panel-body">',
					    			'<ul class="clfy_grouplist"></ul>',
					    		'</div>',
					    	'</div>',
					    	'<div class="panel panel-info clfy_material">',
					    		'<div class="panel-heading">',
					    			'<h3 class="panel-title">素材列表</h3>',
					    	    '</div>',
					    		'<div class="panel-body">',
					    			'<ul class="clfy_ungroup"></ul>',
					    			'<ul class="clfy_hasgroup"></ul>',
					    		'</div>',
					    	'</div>',
					    '</div>'].join('');
		var tmpl_group = '<li title="{name}">{name}<span class="glyphicon glyphicon-minus" title="删除"></span><label>权重：<input type="text" name="weight" value="{weight}" class="clfy_weight"></label></li>';
		var tmpl_material = '<li title="{tags}" id="material_{id}">{name}<span class="glyphicon glyphicon-plus" title="添加"></span></li>';
		$node.append(tmp_box);
		
		//初始化分组列表
		if(semi.utils.isEmpty(op.data.group))op.data.group = [];
		$('.clfy_grouplist').data({names:[]});
		$.each(op.data.group, function(i,n){
			$('.clfy_grouplist').append(tmpl_group.replaceAll('{name}', n.name).replaceAll('{weight}', n.weight)).find('li:last').data('group', n);
			$('.clfy_grouplist').data('names').push(n.name)
		});
		
		//初始化未分组素材
		$.each(op.data.ungroup, function(i,n){
			$('.clfy_ungroup').append(tmpl_material.replaceAll('{id}', n.id).replaceAll('{name}', n.name).replaceAll('{tags}', n.tags.join(','))).find('li:last').data('info', n);
		});
		
		//选择分组
		$(document).on('click', '.clfy_grouplist li', function(){
			var $this = $(this);
			$this.addClass('current').siblings('.current').removeClass('current');
			$('.clfy_hasgroup').empty().attr('group',$this.attr('title'));
			$.each($this.data('group').materials, function(i,n){
				$('.clfy_hasgroup').append(tmpl_material.replaceAll('{id}', n.id).replaceAll('{name}', n.name).replaceAll('{tags}', n.tags.join(',')));
				changeIcon($('.clfy_hasgroup li:last').data('info', n));
			});
		});
		
		//添加素材
		$(document).on('click', '.clfy_ungroup li', function(){
			var $this = $(this), current = $('.clfy_hasgroup').attr('group'), nameArr = $('.clfy_grouplist').data('names');
			
			if(semi.utils.isEmpty(current) || nameArr.indexOf(current)==-1){
				$('.clfy_warning').remove();
				$('.clfy_material .panel-heading').append('<span class="clfy_warning">请先选择一个分组吧！</span>');
				setTimeout(function(){
					$('.clfy_material .panel-heading .clfy_warning').fadeOut(function(){$(this).remove()});
				},1000);
				return;
			}
			changeIcon($this);
			$this.appendTo($('.clfy_hasgroup'));
			
			var group = $('.clfy_hasgroup').attr('group');
			var arr = $('.clfy_grouplist [title='+group+']').data('group').materials;
			arr.pushOnly($this.data('info'));
		});
		
		//删除素材
		$(document).on('click', '.clfy_hasgroup li', function(){
			var $this = $(this);
			changeIcon($this);
			$this.appendTo($('.clfy_ungroup'));
			
			var group = $('.clfy_hasgroup').attr('group');
			var arr = $('.clfy_grouplist [title='+group+']').data('group').materials;
			arr.remove($this.data('info'));
		});
		
		//新增分组
		$(document).on('click', '.clfy_addgroup', function(){
			var name = checkGroupName($(this).siblings('.clfy_newgroup').val()), w=0;
			if($('.clfy_grouplist li').size()<1)w=100;
			$('.clfy_grouplist').append(tmpl_group.replaceAll('{name}', name).replaceAll('{weight}', w)).find('li:last').data('group', {name:name,weight:w,materials:[]});
			//changeIcon($('.clfy_grouplist li:last'));
			$('.clfy_grouplist').data('names').pushOnly(name)
		});
		
		//删除分组
		$(document).on('click', '.clfy_grouplist .glyphicon', function(){
			var $group = $(this).parents('li:first'), name = $group.attr('title');
			$.each($group.data('group').materials, function(i,n){
				$('.clfy_ungroup').append(tmpl_material.replaceAll('{id}', n.id).replaceAll('{name}', n.name).replaceAll('{tags}', n.tags.join(','))).find('li:last').data('info', n);
			});
			$group.remove();
			$('.clfy_grouplist').data('names').remove(name);
		});
		
		
		$(document).on('keyup', '.clfy_weight', function(){
			var $this = $(this), thisval = $this.val();
			if(thisval.length>0 && !/^\d+$/.test(thisval)){
				$this.val(thisval.replace(/\D/g,''));
			}
		});
		
		$(document).on('change', '.clfy_weight', function(){
			var $this = $(this), thisval = parseInt($this.val()), otherval = 0;
			if(thisval>100){
				$this.val(100);
				$('.clfy_weight').not($this).val(0);
				return;
			}
			$('.clfy_weight').not($this).each(function(){
				otherval += parseInt($(this).val());
			});
			if(otherval>100-thisval){
				var _w = otherval-(100-thisval), $last = $('.clfy_weight').not($this).last(), value = parseInt($last.val());
				while(_w-value>0){
					$last.val(0);
					_w-=value;
					$last = $('.clfy_weight').not($this).not($last).last();
					value = parseInt($last.val());
				}
				$last.val(value-_w);
			}else{
				var $last = $('.clfy_weight').not($this).last(), value = parseInt($last.val());
					$last.val(100 - thisval - otherval + value);
			}
		});
		
		
		var changeIcon = function(item){
			if($('.glyphicon',item).hasClass('glyphicon-minus')){
				$('.glyphicon',item).addClass('glyphicon-plus').removeClass('glyphicon-minus')
			}else{
				$('.glyphicon',item).addClass('glyphicon-minus').removeClass('glyphicon-plus')
			}
		}
		
		var checkGroupName = function(name){
			var nameArr = $('.clfy_grouplist').data('names'), txt = '新分组', index = 1;
			if(semi.utils.isEmpty(name)){
				var _name = txt+index;
				while(nameArr.indexOf(_name)!=-1){
					_name = txt+(++index);
				}
				return _name
			}else{
				if(nameArr.indexOf(name)==-1){
					return name
				}else{
					var _name = name+'_'+index;
					while(nameArr.indexOf(_name)!=-1){
						_name = name+'_'+(++index);
					}
					return _name
				}
			}
		}
		
	}
	
	var getData = function(param){
		var dataArr = [];
		$('.clfy_grouplist li').each(function(){
			dataArr.push($(this).data('group'));
		});
		if(typeof param!='undefined'){
			var materials = [];
			$.each(dataArr, function(i,n){
				$.each(n.materials, function(j, m){
					materials.push({group:n.name, weight:n.weight, material_id:m.id});
				});
			});
			return materials;
		}else{
			$.each(dataArr, function(i,n){
				var materials = [];
				$.each(n.materials, function(j, m){
					materials.push(m.id);
				});
				n.materials = materials;
			});
			return dataArr;
		}
		
	}
	
	return {
		init:initClassify,
		get: getData
	}
});